package com.yenlien.traceyun.produce.controller;


import com.alibaba.fastjson.JSONObject;
import com.yenlien.traceyun.business.entity.ProcessDetail;
import com.yenlien.traceyun.business.entity.ProduceManage;
import com.yenlien.traceyun.business.entity.ProduceManageTech;
import com.yenlien.traceyun.business.interceptor.UserAuthUtil;
import com.yenlien.traceyun.produce.service.ManageService;
import com.yenlien.traceyun.webase.controller.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * 生产管理控制层
 */
@RestController
@RequestMapping(value = "/manage")
public class ManageController extends BaseController {

    @Autowired
    private ManageService manageService;

    /**
     * 新增生产管理
     */
//    @PostMapping("/save")
//    public Object save(@RequestBody JSONObject params){
//        ProduceManage producemanage = params.getJSONObject("produceManage").toJavaObject(ProduceManage.class);
//        return success(manageService.save(producemanage));
//    }
    @PostMapping("/save")
    public Object save(ProduceManage producemanage, HttpServletRequest request) {
        List<MultipartFile> files = new ArrayList<>();
        if (!StringUtils.isEmpty(((MultipartHttpServletRequest) request).getFile("files"))) {
            files = ((MultipartHttpServletRequest) request).getFiles("files");
        }
        return success(manageService.save(producemanage, files));
    }

    // 给生产管理添加 工序工艺集合
    @PostMapping("/saveTech")
    public Object saveTech(List<ProduceManageTech> produceManageTechList, HttpServletRequest request) {
        List<MultipartFile> files = new ArrayList<>();
        if (!StringUtils.isEmpty(((MultipartHttpServletRequest) request).getFile("files"))) {
            files = ((MultipartHttpServletRequest) request).getFiles("files");
        }
        return success(manageService.saveTech(produceManageTechList, files));
    }

    // 给生产管理添加 成品
    @PostMapping("/saveProc")
    public Object saveProc(@RequestBody List<ProcessDetail> processDetailList) {
        return success(manageService.saveProc(processDetailList));
    }


    /**
     * 查询生产管理
     *
     * @param params
     * @return
     */
    @PostMapping("/inquire")
    public Object inquire(@RequestBody JSONObject params) {
        Integer page = params.getInteger("page");
        String type = params.getString("type");
        if (page > 0) {
            Integer page1 = page - 1;
            return buildPage(manageService.select(page1, type));
        } else {
            return fail("页面加载错误");
        }
    }

    // 分页查询成品
    @PostMapping("/getProcessDetail")
    public Object getProcessDetail(@RequestBody JSONObject params) {
        Integer page = params.getInteger("page");
        String type = params.getString("type");
        String searchName = params.getString("searchName");
        String companyId = UserAuthUtil.getLoginUser().getCompanyId();
        Integer page1 = page - 1;
        return buildPage(manageService.getProcessDetail(page1, type, companyId, searchName));
    }


    /**
     * 搜索栏
     *
     * @param params
     * @return
     */
    @PostMapping("/search")
    public Object search(@RequestBody JSONObject params) {
        return success(manageService.searchPage(params.getInteger("page"), params.getString("searchName"), params.getString("type"),
                UserAuthUtil.getLoginUser().getCompanyId()));
    }

    /**
     * 根据id查询生产管理
     *
     * @param params
     * @return
     */
    @PostMapping("/selectId")
    public Object selectId(@RequestBody JSONObject params) {
        String id = params.getString("id");
        if (id.length() > 0) {
            return success(manageService.selectId(id));
        } else {
            return fail("生产信息不存在");
        }
    }

    /**
     * 根据id修改生产管理
     *
     * @param params
     * @return
     */
    @PostMapping("/update")
    public Object update(@RequestBody JSONObject params) {
        ProduceManage produceManage = params.getJSONObject("produceManage").toJavaObject(ProduceManage.class);
        manageService.update(produceManage);
        return success("success");
    }

    /**
     * 根据id删除基本信息和原料辅料信息
     *
     * @param params
     * @return
     */
    @PostMapping("/delete")
    public Object delete(@RequestBody JSONObject params) {
        ProduceManage produceManage = params.getJSONObject("produceManage").toJavaObject(ProduceManage.class);
        manageService.delete(produceManage);
        return success("success");
    }
}
